﻿Imports DAL
Imports System.Linq
Public Class FrmEditionPlage

#Region "properties"
    Private DefaultDate As Date = New Date(2000, 1, 1, 0, 0, 0)
    Private ProgValue As Boolean = False
    Private isloading As Boolean

    Public Property Titre() As String
        Get
            Return Tb_Titre.Text
        End Get
        Set(ByVal value As String)
            Tb_Titre.Text = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return Tb_Description.Text
        End Get
        Set(ByVal value As String)
            Tb_Description.Text = value
        End Set
    End Property

    Public Property QuartArriere() As Boolean
        Get
            Return Ckb_QuartArriere.Checked
        End Get
        Set(ByVal value As Boolean)
            Ckb_QuartArriere.Checked = value
        End Set
    End Property
    Private _Accompagnement As Boolean = False
    Public Property Accompagnement() As Boolean
        Get
            Return _Accompagnement
        End Get
        Set(value As Boolean)
            _Accompagnement = value
        End Set
    End Property


    Public Property DateDebut() As TimeSpan
        Get
            Return New TimeSpan(Dtp_Debut.Value.TimeOfDay.Ticks)
        End Get
        Set(ByVal value As TimeSpan)
            Dtp_Debut.Value = DefaultDate.AddTicks(value.Ticks)
        End Set
    End Property

    Public Property Duree() As TimeSpan
        Get
            Return New TimeSpan(Dtp_Duree.Value.TimeOfDay.Ticks)
        End Get
        Set(ByVal value As TimeSpan)
            Dtp_Duree.Value = DefaultDate.AddTicks(value.Ticks)
        End Set
    End Property

    Public Property Couleur() As Color
        Get
            Return Pb_CouleurPlage.BackColor
        End Get
        Set(ByVal value As Color)
            Pb_CouleurPlage.BackColor = value
        End Set
    End Property
    Enum TypeEdition
        AjoutPlageStock
        ModificationPlage
        LecturePlage
    End Enum
    Enum TypeEditionTache
        Ajout
        Modification
        Visionement
    End Enum
    Private _modeEdition As TypeEdition = TypeEdition.AjoutPlageStock
    Public Property ModeEdition() As TypeEdition
        Get
            Return _modeEdition
        End Get
        Set(ByVal value As TypeEdition)
            _modeEdition = value
            If value = TypeEdition.AjoutPlageStock Then
                Lbl_DropDownTache.Visible = False
                Lbl_QuartArriere.Visible = False
                Ckb_QuartArriere.Visible = False
            Else
                Lbl_DropDownTache.Visible = True
                Lbl_QuartArriere.Visible = True
                Ckb_QuartArriere.Visible = True
            End If

            If value = TypeEdition.LecturePlage Then
                ToggleGeneralEdit(False)
            Else
                ToggleGeneralEdit(True)
            End If
        End Set
    End Property
    Private _ValueModified As Boolean = False
    Public ReadOnly Property ValueModified() As Boolean
        Get
            Return _ValueModified
        End Get
    End Property

    Private Sub ToggleGeneralEdit(ByVal state As Boolean)
        Tb_Titre.Enabled = state
        Tb_Description.Enabled = state
        Dtp_Duree.Enabled = state
        Dtp_Debut.Enabled = state
        Dtp_Fin.Enabled = state
        Btn_ColorPicker.Enabled = state
        TableLayoutPanel1.Visible = state
        Ckb_QuartArriere.Enabled = state
    End Sub

    Private ModeEditionTache As TypeEditionTache = TypeEditionTache.Visionement
    Private _Beneficiaire As String = String.Empty
    Public Property Beneficiaire() As String
        Get
            Return _Beneficiaire
        End Get
        Set(ByVal value As String)
            _Beneficiaire = value
        End Set
    End Property

    Private _listeTache As List(Of Tache)
    Public Property ListeTache() As List(Of Tache)
        Get
            Return _listeTache
        End Get
        Set(ByVal value As List(Of Tache))
            _listeTache = value
            RemplirListViewTache()
        End Set
    End Property
    Private DebutPrecedent As Date

    Private SelectedTacheID As Integer = -1
#End Region

    Private Sub DTPDebut_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Debut.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Not Dtp_Debut.Value.TimeOfDay = DefaultDate.AddMinutes(1439).TimeOfDay Then
                    Dim newFin = Dtp_Debut.Value.AddTicks(Dtp_Duree.Value.TimeOfDay.Ticks)
                    If newFin.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                        Dtp_Fin.Value = DefaultDate.AddMinutes(1439)
                        Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                    Else
                        Dtp_Fin.Value = newFin
                    End If
                Else
                    Dtp_Debut.Value = DebutPrecedent
                End If
                IsLoading = False
            End If
            DebutPrecedent = Dtp_Debut.Value
        End If
    End Sub

    Private Sub DTPDuree_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Duree.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                Dim newFin = Dtp_Debut.Value.AddTicks(Dtp_Duree.Value.TimeOfDay.Ticks)
                If newFin.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                    Dtp_Fin.Value = DefaultDate.AddMinutes(1439)
                    Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                Else
                    Dtp_Fin.Value = newFin
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub DTPFin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Fin.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Dtp_Fin.Value.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                    Dtp_Fin.Value = Dtp_Debut.Value.AddMinutes(1)
                Else
                    Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub FrmEditionPlage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DefaultDate = New Date(2000, 1, 1, 0, 0, 0)
        'Dtp_Debut.Value = DefaultDate
        Dtp_Debut.MinDate = DefaultDate
        Dtp_Debut.MaxDate = DefaultDate.AddHours(24)

        Dtp_Fin.MinDate = DefaultDate
        Dtp_Fin.MaxDate = DefaultDate.AddHours(24)
        Gb_Tache.Visible = False
        Me.Height = 100
        If _Accompagnement Then
            Lbl_QuartArriere.Text = "Bénéficiaire :"
            LblAccomp.Text = _Beneficiaire
            Ckb_QuartArriere.Visible = False
            LblAccomp.Visible = True
        Else
            LblAccomp.Text = String.Empty
        End If
        'Dtp_Fin.Value = DefaultDate.AddHours(2)
    End Sub

    Private Sub Btn_ColorPicker_Click(sender As System.Object, e As System.EventArgs) Handles Btn_ColorPicker.Click
        Using diag As New FrmColorPicker()
            diag.Value = Pb_CouleurPlage.BackColor
            If diag.ShowDialog = DialogResult.OK Then
                Pb_CouleurPlage.BackColor = diag.Value
                If ModeEdition = TypeEdition.ModificationPlage AndAlso Not ValueModified Then
                    _ValueModified = True
                End If
            End If
        End Using


    End Sub

    Private Sub btnAccepter_Click(sender As System.Object, e As System.EventArgs) Handles btnAccepter.Click
        ErrorProvider1.Clear()
        If Titre.Length < 1 Or Titre.Length > 50 Then
            ErrorProvider1.SetError(Tb_Titre, "Veuillez entrer un titre entre 1 et 50 caractère.")
        End If
        If Duree.TotalMinutes < 1 Then
            ErrorProvider1.SetError(Dtp_Duree, "Veuillez entrer une durée de plus d'une minute.")
        End If
        If Not ModeEditionTache = TypeEditionTache.Visionement Then
            MessageBox.Show("Veuillez terminer la modification des tâches avant de quitter.", "Action impossible", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return
        End If

        If ErrorProvider1.GetError(Tb_Titre) = String.Empty And ErrorProvider1.GetError(Dtp_Duree) = String.Empty Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If

    End Sub

    Private Sub btnAnnuler_Click(sender As System.Object, e As System.EventArgs) Handles btnAnnuler.Click

    End Sub

    Private Sub Lbl_DropDownTache_Click(sender As System.Object, e As System.EventArgs) Handles Lbl_DropDownTache.Click
        Lbl_DropDownTache.Image = IIf(Not Gb_Tache.Visible, My.Resources.up, My.Resources.down)
        Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
        AnimationShowHideTache(Not Gb_Tache.Visible)
        Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
    End Sub

    Private Sub AnimationShowHideTache(ByVal visible As Boolean)
        If Not visible Then
            Gb_Tache.Visible = visible
        End If
        'For i = 1 To 160 Step 5
        '    Me.Height += IIf(visible, 5, -5)
        '    Me.Refresh()
        '    Threading.Thread.Sleep(3)
        'Next
        Gb_Tache.Visible = visible
    End Sub


    Private Sub ToggleTacheEditing(ByVal state As Boolean)
        Tb_TitreTache.Enabled = state
        Tb_DescTache.Enabled = state
        Dtp_DureeTache.Enabled = state
    End Sub

    Private Sub Lv_Taches_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Lv_Taches.SelectedIndexChanged
        If ModeEditionTache = TypeEditionTache.Visionement Then
            If Lv_Taches.SelectedItems.Count = 1 Then
                SelectedTacheID = Lv_Taches.SelectedIndices(0)
                DisplayTache(SelectedTacheID)

                Btn_DeleteTache.Enabled = True
                Btn_EditTache.Enabled = True
            Else
                Btn_DeleteTache.Enabled = False
                Btn_EditTache.Enabled = False
                SelectedTacheID = -1
                ClearTacheDisplay()
            End If
        End If
        
    End Sub

    Private Sub DisplayTache(ByVal id As Integer)
        Tb_TitreTache.Text = ListeTache(id).Nom
        Tb_DescTache.Text = ListeTache(id).Description
        If ListeTache(id).Duree > 0 Then
            Dtp_DureeTache.Value = DefaultDate.AddMinutes(ListeTache(id).Duree)
            Dtp_DureeTache.Checked = True
        Else
            Dtp_DureeTache.Value = DefaultDate
            Dtp_DureeTache.Checked = False
        End If

    End Sub

    Private Sub ClearTacheDisplay()
        Tb_TitreTache.Text = String.Empty
        Tb_DescTache.Text = String.Empty
        Dtp_DureeTache.Checked = False
    End Sub

    Private Sub Btn_EditTache_Click(sender As System.Object, e As System.EventArgs) Handles Btn_EditTache.Click
        If Btn_EditTache.Text = "Modifier" Then
            ModeEditionTache = TypeEditionTache.Modification
            Btn_NewTache.Text = "Enregistrer"
            Btn_EditTache.Text = "Annuler"
            Btn_DeleteTache.Visible = False
            ToggleTacheEditing(True)
        Else 'Annuler

            ModeEditionTache = TypeEditionTache.Visionement
            Btn_NewTache.Text = "Nouveau"
            Btn_EditTache.Text = "Modifier"
            Btn_DeleteTache.Visible = True
            If Lv_Taches.SelectedItems.Count = 1 Then
                DisplayTache(Lv_Taches.SelectedIndices(0))
                Btn_DeleteTache.Enabled = True
            Else
                ClearTacheDisplay()
                Btn_EditTache.Enabled = False
                Btn_DeleteTache.Enabled = False
            End If
            ErrorProvider1.SetError(Tb_TitreTache, String.Empty)
            ToggleTacheEditing(False)
        End If

    End Sub

    Private Sub Btn_NewTache_Click(sender As System.Object, e As System.EventArgs) Handles Btn_NewTache.Click
        If Btn_NewTache.Text = "Nouveau" Then
            ModeEditionTache = TypeEditionTache.Ajout
            Btn_NewTache.Text = "Enregistrer"
            Btn_EditTache.Text = "Annuler"
            Btn_EditTache.Enabled = True
            Btn_DeleteTache.Visible = False
            ClearTacheDisplay()
            ToggleTacheEditing(True)
        Else
            If ModeEditionTache = TypeEditionTache.Ajout Then
                If Tb_TitreTache.Text.Trim() = String.Empty Then
                    ErrorProvider1.SetError(Tb_TitreTache, "Veuillez entrer un titre")
                    Return
                Else
                    ErrorProvider1.SetError(Tb_TitreTache, String.Empty)
                End If
                Dim task As New Tache
                task.Nom = Tb_TitreTache.Text
                task.Description = Tb_DescTache.Text
                If Dtp_DureeTache.Checked Then
                    task.Duree = New TimeSpan(Dtp_DureeTache.Value.TimeOfDay.Ticks).TotalMinutes
                Else
                    task.Duree = 0
                End If

                _listeTache.Add(task)
                SelectedTacheID = _listeTache.Count - 1
            Else 'Modification
                If Tb_TitreTache.Text.Trim() = String.Empty Then
                    ErrorProvider1.SetError(Tb_TitreTache, "Veuillez entrer un titre")
                    Return
                Else
                    ErrorProvider1.SetError(Tb_TitreTache, String.Empty)
                End If
                Dim task As Tache = _listeTache(SelectedTacheID)
                task.Nom = Tb_TitreTache.Text
                task.Description = Tb_DescTache.Text
                If Dtp_DureeTache.Checked Then
                    task.Duree = New TimeSpan(Dtp_DureeTache.Value.TimeOfDay.Ticks).TotalMinutes
                Else
                    task.Duree = 0
                End If

            End If
            If ModeEdition = TypeEdition.ModificationPlage AndAlso Not ValueModified Then
                _ValueModified = True
            End If
            ModeEditionTache = TypeEditionTache.Visionement
            Btn_NewTache.Text = "Nouveau"
            Btn_EditTache.Text = "Modifier"
            Btn_DeleteTache.Visible = True
            RemplirListViewTache()
            ToggleTacheEditing(False)
        End If
    End Sub

    Private Sub RemplirListViewTache()
        Lv_Taches.Items.Clear()
        For Each task As Tache In _listeTache
            Dim itm As New ListViewItem(task.Nom)
            If task.Duree > 0 Then
                itm.SubItems.Add(DefaultDate.AddTicks(TimeSpan.FromMinutes(task.Duree).Ticks).ToString("H' h 'mm"))
            Else
                itm.SubItems.Add(String.Empty)
            End If

            itm.ToolTipText = task.Nom
            Lv_Taches.Items.Add(itm)
        Next
    End Sub

    Private Sub Btn_DeleteTache_Click(sender As System.Object, e As System.EventArgs) Handles Btn_DeleteTache.Click
        _listeTache.RemoveAt(SelectedTacheID)
        ClearTacheDisplay()
        RemplirListViewTache()
        If ModeEdition = TypeEdition.ModificationPlage AndAlso Not ValueModified Then
            _ValueModified = True
        End If
    End Sub

    Private Sub Form_Item_ValueChanged(sender As System.Object, e As System.EventArgs) _
        Handles Tb_Titre.TextChanged, Tb_Description.TextChanged, Dtp_Debut.ValueChanged, Dtp_Duree.ValueChanged, Dtp_Fin.ValueChanged
        If ModeEdition = TypeEdition.ModificationPlage AndAlso Not ValueModified Then
            _ValueModified = True
        End If
    End Sub

    'dut à l'initialisation des champs avant l'ouverture du dialogue, la propriétée _ValueModified devient vraie
    'la remettre a faux lors de l'ouverture
    Public Overloads Function ShowDialog() As DialogResult
        _ValueModified = False
        Return MyBase.ShowDialog()
    End Function

    Private Sub Ckb_QuartArriere_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles Ckb_QuartArriere.CheckedChanged
        If ModeEdition = TypeEdition.ModificationPlage AndAlso Not ValueModified Then
            _ValueModified = True
        End If
    End Sub
End Class